package Er

import (
	"errors"
	"fmt"
	dm "db2s/Dm"
	greatdb "db2s/Gr"
	sqlserver "db2s/Ms"
	mysql "db2s/My"
	oracle "db2s/Or"
	"reflect"
	"strings"
)

type TableInfoMeta struct {
	DBType string
}

var (
	metaApi = map[string]any{
		"mysql":     &mysql.TableInfoMeta{},
		"oracle":    &oracle.TableInfoMeta{},
		"cluster":   &greatdb.TableInfoMeta{},
		"sqlserver": &sqlserver.TableInfoMeta{},
		"dm": &dm.TableInfoMeta{},
	}
)

func MetaInfo(s TableInfoMeta) (t TablesMetaInfoEr, err error) {
	mutex.Lock()
	defer mutex.Unlock()
	if _, ok := dbTypeInterFuncRef[s.DBType]; !ok {
		return nil, errors.New(fmt.Sprintf("input database type error. input is {%v} What is expected is {%v}", s.DBType, func() string {
			var p []string
			for k := range dbTypeInterFuncRef {
				p = append(p, k)
			}
			return strings.Join(p, ",")
		}()))
	}
	if dbTypeInterFuncRef[s.DBType].Kind() == reflect.Func {
		args := []reflect.Value{
			reflect.ValueOf(s),
			reflect.ValueOf(metaApi[s.DBType]),
		}
		i := dbTypeInterFuncRef[s.DBType].Call(args)
		for ii := 0; ii < len(i); ii++ {
			t = i[ii].Interface().(TablesMetaInfoEr)
		}
	}
	return
}

